from selenium import webdriver
import os
import time
import unittest
import HTMLTestRunner


def open_browser(browser="chrome"):
    if browser == "chrome":
        return webdriver.Chrome()
    elif browser == "firefox":
        return webdriver.Firefox()
    elif browser == "edge":
        return webdriver.Edge()
    else:
        print("不支持该浏览器,请选择:chrome,firefox,edge")


def output_report(dir_path, case="test*"):
    project_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    report_filename = time.strftime("%Y-%m-%d_%H-%M-%S") + "_HTMLReport.html"
    file_path = os.path.join(project_path, "report", report_filename)
    print(file_path)
    loader = unittest.defaultTestLoader.discover(dir_path, pattern=case)
    with open(file_path, "wb") as w:
        runner = HTMLTestRunner.HTMLTestRunner(
            title="自动化测试报告",
            description="报告描述",
            stream=w,
            verbosity=2
        )
        runner.run(loader)


if __name__ == '__main__':
    case_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "case/back_stage_case")
    print(case_path)
    output_report(case_path)
